import { getLyric, radioStationUrl } from '@/api'
export default ({
    namespaced: true,
    state: {
        musicList: [{
            artist: "",
            cover: "",
            lrc: "",
            name: "請選擇音樂",
            url: "",
        }]
    },
    mutations: {
        change_musicList(start, data) {
            start.musicList = data;
        },
        unshiftmusicList(start, { musicObj, that }) {
            let flag = true;
            start.musicList.forEach((item, index) => {
                if (musicObj.id === item.id) {
                    flag = false;
                    let obj = item;
                    start.musicList.splice(index, 1);
                    start.musicList.unshift(obj);
                }
            })
            if (flag) start.musicList.unshift(musicObj);
            that.$bus.$emit("playCurrentMusic");
        },
        //删除音乐
        delMusic(start, data) {
            start.musicList.forEach((item, index) => {
                if (data === item.id) {
                    start.musicList.splice(index, 1);
                }
            })
        },
        mergeMusicList(start, data) {
            let obj = {};
            start.musicList = [...start.musicList, ...data].reduce((cur, next) => {
                obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
                return cur;
            }, [])
            console.log(start.musicList);
        },
        pushMusic(start, musicObj) {
            let flag = true;
            start.musicList.forEach((item, index) => {
                if (musicObj.id === item.id) flag = false;
            })
            if (flag) start.musicList.push(musicObj);
        },
    },
    actions: {
        //播放当前音乐列表
        async songSheetPlay({ dispatch, commit }, data) {//修改音乐列表
            let arr = []
            for (let i = 0; i < data.length; i++) {
                let item = data[i];
                await getLyric({ id: data[i].id }).then((res) => {
                    arr.push({
                        id: data[i].id,
                        name: item.name,
                        artist: item?.ar[0].name || '',
                        url: 'https://music.163.com/song/media/outer/url?id=' + item.id + '.mp3',
                        cover: item?.al.picUrl, // prettier-ignore
                        lrc: res?.lrc?.lyric || '',
                    })
                })
            }
            commit('change_musicList', arr)
        },
        //播放当前歌曲
        async unshiftMusic({ commit }, { data, that }) {
            await getLyric({ id: data.id }).then((res) => {
                let obj = {
                    musicObj: {
                        id: data.id,
                        name: data.name,
                        artist: data?.ar[0].name || '',
                        url: 'https://music.163.com/song/media/outer/url?id=' + data.id + '.mp3',
                        cover: data?.al.picUrl, // prettier-ignore
                        lrc: res?.lrc?.lyric || '',
                    },
                    that
                }
                commit('unshiftmusicList', obj)
            })
        },
        //删除当前音乐
        delMusicAction({ commit }, data) {
            commit('delMusic', data)
        },
        //合并音乐列表
        async addMusicList({ dispatch, commit }, data) {
            let arr = []
            for (let i = 0; i < data.length; i++) {
                let item = data[i];
                await getLyric({ id: data[i].id }).then((res) => {
                    arr.push({
                        id: data[i].id,
                        name: item.name,
                        artist: item?.ar[0].name || '',
                        url: 'https://music.163.com/song/media/outer/url?id=' + item.id + '.mp3',
                        cover: item?.al.picUrl, // prettier-ignore
                        lrc: res?.lrc?.lyric || '',
                    })
                })
            }
            commit('mergeMusicList', arr)
        },
        //添加音乐
        async pushMusicAction({ commit }, data) {
            await getLyric({ id: data.id }).then((res) => {
                commit('pushMusic', {
                    id: data.id,
                    name: data.name,
                    artist: data?.ar[0].name || '',
                    url: 'https://music.163.com/song/media/outer/url?id=' + data.id + '.mp3',
                    cover: data?.al.picUrl, // prettier-ignore
                    lrc: res?.lrc?.lyric || '',
                })
            })
        },
        //直接播放电台
        async unshiftRadioStation({ commit }, { data, that }) {
            await radioStationUrl({ id: data.mainTrackId }).then((res) => {
                let obj = {
                    musicObj: {
                        id: data.mainTrackId,
                        name: data.name,
                        artist: data?.dj.brand || '',
                        url: res?.data[0]?.url,
                        cover: data?.blurCoverUrl, // prettier-ignore
                        lrc: '[00:00.000] 电台节目，无歌词',
                    },
                    that
                }
                commit('unshiftmusicList', obj)
            })
        },
    }
})
